#ifndef COLLISIONOBJECT_H
#define COLLISIONOBJECT_H

#include <vector>
#include "Vector2df.h"
#include "PenjinTypes.h"
#include "CollisionRegion.h"
using namespace std;

//----------------------------
// by foxblock for Wandor - Nov 03 2009
// An object which stores tiles a unit collides with
// and provides functions to analyse the situation
//----------------------------

class CollisionObject
{
public:
    CollisionObject();
    ~CollisionObject();

    /// General functions
    Vector2df calculateTotalCorrection();                                   // Goes through all stored vectors and saves and returns a vector with the highest correction values
    Vector2df getTotalCorrection() const;                                   // Returns the total corrections vector, calculateTotalCorrection needs to have been called
    Vector2df getTileX(CRuint index) const;                                 // Returns the stored tile with the passed index
    Vector2df getTileY(CRuint index) const;                                 // Returns the stored tile with the passed index
    Vector2df getCorrectionX(CRuint index) const;                           // Returns the correction of a stored tile with the passed index
    Vector2df getCorrectionY(CRuint index) const;                           // Returns the correction of a stored tile with the passed index
    bool getCorrection(const Vector2df tile, Vector2df &correction) const;  // Searches for a tile and returns true if found and the correction vector
    int getSizeX() const;                                                   // Returns the number of tiles stored by collision checking in horizontal direction
    int getSizeY() const;                                                   // Returns the number of tiles stored by collision checking in vertical direction
    int hasTileX(const Vector2df tile) const;                               // Searches for a tile in the X-vector and returns the index if found or -1 if not
    int hasTileY(const Vector2df tile) const;                               // Searches for a tile in the Y-vector and returns the index if found or -1 if not
    void addTileX(const Vector2df tile, const Vector2df correction);        // Add a tile to the X-vector - after checking whether it's already present in the other vector
    void addTileY(const Vector2df tile, const Vector2df correction);        // Add a tile to the Y-vector - after checking whether it's already present in the other vector
    void clear();                                                           // Erase all stored tiles and reset variables

    bool isInWater;
    int slantCollision;

    /// Analysing funtions
    bool isOnPlatformEdge(const Vector2df position, const Vector2df speed) const;
    bool isOnGround(const Vector2df position) const;
    bool isInAir(const Vector2df position) const;
    Vector2df getCollisionTile(const Vector2df position, const SimpleDirection dir=diNONE) const;
    SimpleDirection getCollisionDirection(const Vector2df position) const;

private:
    Vector2df getTile(const vector< vector< Vector2df > > * const tiles, CRuint index) const;
    int hasTile(const vector< vector< Vector2df > > * const tiles, const Vector2df tile) const;
    Vector2df getCorrection(const vector< vector< Vector2df > > * const tiles, CRuint index) const;
    void addTile(vector< vector< Vector2df > > * addTiles, vector< vector< Vector2df > > * checkTiles, const Vector2df tile, const Vector2df correction);
    Vector2df getNearestCollisionTile(const vector< vector< Vector2df > > * tiles, const Vector2df position) const;

    vector< vector< Vector2df > > m_tilesX;
    vector< vector< Vector2df > > m_tilesY;
    Vector2df m_correction;
};

#endif // COLLISIONOBJECT_H
